//
//  XBFlowLayout.m
//  寻宝之旅
//
//  Created by LyuBook on 16/4/4.
//  Copyright © 2016年 lvdingze. All rights reserved.
//

#import "XBFlowLayout.h"
@interface XBFlowLayout ()
//存储所有cell布局属性的数组
@property (nonatomic,strong) NSMutableArray *attsArr;
@property (nonatomic, assign) CGFloat lastObjectHeight;
@end

@implementation XBFlowLayout
-(void)prepareLayout{
    [super prepareLayout];
    //如果是第零组就返回
    if (self.count == 0) {
        return ;
    }
    
    [self setupLayout]; 
    
}

-(void)setupLayout
{
    CGFloat width = self.collectionView.bounds.size.width;
    CGFloat margin = 10;
    
    CGFloat height0 = 260;
    //第零组
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    UICollectionViewLayoutAttributes *att1 = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    att1.frame = CGRectMake(0, 0, width, height0);
    [self.attsArr addObject:att1];
    //第一组
    CGFloat height1 = 194;
    NSInteger totalCount1 = [self.collectionView numberOfItemsInSection:1];
    for (int i = 0; i < totalCount1; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:1];
        UICollectionViewLayoutAttributes *att=[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        //计算第一组Y值
        CGFloat cellY = i *(height1+margin) + (height0+margin);
        CGFloat cellX = 0;
        att.frame = CGRectMake(cellX, cellY, width, height1);
        [self.attsArr addObject:att];
    }
    UICollectionViewLayoutAttributes *lastAtt1 = self.attsArr.lastObject;
    //计算第一组最后一个Y值（已经加上间隔margin）
    CGFloat sectionHeight1 = lastAtt1.frame.origin.y + height1 + margin;
    //判断第一组元素是否为空
    if (totalCount1 == 0) {
        sectionHeight1 = lastAtt1.frame.origin.y + height0 + margin;
    }
    //第二组
    NSInteger totalCount2 = [self.collectionView numberOfItemsInSection:2];
    for (int i = 0; i < totalCount2; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:2];
        UICollectionViewLayoutAttributes *att=[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        CGFloat cellX = 0;
        CGFloat cellY = i *(height1+margin) + sectionHeight1;
        att.frame = CGRectMake(cellX, cellY, width, height1);
        [self.attsArr addObject:att];
    }
    
    CGFloat sectionHeadHeight = 64;
    UICollectionViewLayoutAttributes *lastAtt2 = self.attsArr.lastObject;
    CGFloat sectionHeight2  = lastAtt2.frame.origin.y + height1 + margin;
    
    
    //打印第二高度，有两个结果，第一个结果是0组的时候  1076/1448
//        NSLog(@"%f",sectionHeight2);
        //添加头部view的布局属性
        NSIndexPath *headerPath=[NSIndexPath indexPathForItem:0 inSection:3];
        UICollectionViewLayoutAttributes *headerAtt=[UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:headerPath];
        
        headerAtt.frame=CGRectMake(0,sectionHeight2,width,sectionHeadHeight);
        [self.attsArr addObject:headerAtt];

    
    //第三组
    CGFloat height3 = 228;
    NSInteger totalCount3 = [self.collectionView numberOfItemsInSection:3];
    for (int i = 0; i < totalCount3; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:3];
        UICollectionViewLayoutAttributes *att=[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        CGFloat width = (self.collectionView.bounds.size.width - 3*margin)* 0.5;
        CGFloat cellX = margin + (i%2)* (width+margin);
        //二组和三组间距
        CGFloat cellY = (sectionHeight2 + sectionHeadHeight + 8) + (i/2)*(height3+ margin);
        att.frame = CGRectMake(cellX, cellY, width, height3);
        [self.attsArr addObject:att];
    }
    
    UICollectionViewLayoutAttributes *lastAtt3 = self.attsArr.lastObject;
    CGFloat sectionHeight3 = lastAtt3.frame.origin.y + height3 + 8;
//        NSLog(@"%f",sectionHeight3);
    
        //添加尾部view的布局属性 2336 2744
        NSIndexPath *footerPath=[NSIndexPath indexPathForItem:0 inSection:3];
        UICollectionViewLayoutAttributes *footerAtt=[UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:footerPath];
        footerAtt.frame=CGRectMake(0,sectionHeight3,width,sectionHeadHeight);
        [self.attsArr addObject:footerAtt];
    
    
    UICollectionViewLayoutAttributes *lastAtt4 = self.attsArr.lastObject;
    self.lastObjectHeight = lastAtt4.frame.origin.y + sectionHeadHeight + 8;
//    NSLog(@"尾部f",self.lastObjectHeight);
}

//调用方法在确定滚动范围
- (CGSize) collectionViewContentSize
{
    [super collectionViewContentSize];
    return CGSizeMake(0, self.lastObjectHeight);
}

//系统的布局数组我们并不需要，我们需要自己重新计算每一个cell的布局属性，。返回给collectionView使用
- (NSArray <UICollectionViewLayoutAttributes *> *) layoutAttributesForElementsInRect:(CGRect)rect
{
    [super layoutAttributesForElementsInRect:rect];
    
    return self.attsArr;
}
- (NSMutableArray *)attsArr
{
    if (_attsArr==nil) {
        _attsArr=[NSMutableArray array];
    }
    return _attsArr;
}

@end
